import baseUrl from "./baseUrl"
import qs from "./qs"

var request = function(config){
  return new Promise((resolve,rejected)=>{
    var setting = {
      url: baseUrl + config.url,
      method:config.method ? (config.method).toUpperCase() :  "GET",
      // method: (config.method).toUpperCase() || "GET",
      data:config.data,
      header:{
        // "content-type":"application/x-www-form-urlencoded"
		token:uni.getStorageSync("token") ? uni.getStorageSync("token") : ""
      },
      timeout:5000,
      success(res){
		  if(res.data.success == true){
			  resolve(res.data)  //.data为某个项目需要，看情况删除
		  }else{
			  uni.showToast({
			  	title:res.data.message
			  })
			  rejected(res)
		  }
        
      },
      fail(res){
        rejected(res)
      }
    }
    // if(config.method){
    //   if(config.method.toUpperCase() == "POST"){
    //     setting.header["content-type"] = "application/x-www-form-urlencoded";
    //     setting.data = qs.stringify(setting.data, { allowDots: true})
    //   }else{
    //     setting.header["content-type"] = "application/json"
    //   }
    // }else{
    //   setting.header["content-type"] = "application/json"
    // }
    uni.request(setting)
  })
  
};
export default request
